#!/bin/bash

PATH=/sbin:/usr/sbin:/bin:/usr/bin:.
export PATH

. /nemonico/BOOT
. /etc/scripts/env
. /etc/scripts/utils.fn

set +eE

if [ "`echo $BOOT_NET_INTERFACE|grep -i dhcp`" ]; then
	isdhcp=Y	
else
	ip="`echo $BOOT_NET_INTERFACE | cut -f1 -d";"`"
	netmask="`echo $BOOT_NET_INTERFACE | cut -f2 -d";"`"
	gateway="`echo $BOOT_NET_INTERFACE | cut -f3 -d";"`"
	driver=$BOOT_NET_DEVICE_DRIVERS
fi


stop ()
{
	if [ "$isdhcp" ]; then
		# release lease
		if [ -s /tmp/udhcpc.pid ]; then
			kill -USR2 `cat /tmp/udhcpc.pid` 2>/dev/null
			kill -9 `cat /tmp/udhcpc.pid` 2>/dev/null
		fi
		killall -q -9 sshd  2>/dev/null
		ifconfig eth0 0.0.0.0
		ifconfig eth0 down
	else
		ifconfig eth0 0.0.0.0
		ifconfig eth0 down
	fi
	killall -9 sshd 2>/dev/null
}

start ()
{
	_generate_sshdkey

	if [ "$isdhcp" ]; then
		udhcpc -i eth0 -p /tmp/udhcpc.pid -b 1>/dev/null 
		echo "Started UDHCP on eth0."
	else
		ifconfig eth0 $ip netmask $netmask 1>/dev/null
		route add default gw $gateway 1>/dev/null
		echo "nameserver $resolver" > /etc/resolv.conf
		echo -e "\n\nNework: eth0:$ip,$netmask,$gateway,$resolver\n\n" > /tmp/interface
	fi

	/usr/sbin/sshd -p ${BOOT_NET_SSH_PORT:-400} -e
	echo "Started SSHD on $BOOT_NET_SSH_PORT."
	# pseudo entropy :)
	echo "Helping /dev/urandom."
	/entropy_generator.sh
}

restart ()
{
	stop
	start
}

case "$1" in
	'start')
		start 
		set -eE
		;;
	'stop')
		stop 
		set -eE
		;;
	'restart')
		restart
		set -eE
		;;
esac

# vim: ts=4 syn=sh
# network.wired: end
